package PVandUv;

import bean.UserBehavior;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.stream.Stream;


/**
 * @author Spring_Hu
 * @date 2021/10/12 20:53
 */
public class PV {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> ds = env.readTextFile("test/UserBehavior.csv");

        env.setParallelism(2);

        ds.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String value) throws Exception {
                return "pv".equals(value.split(",")[3]);
            }
        }).map(new MapFunction<String, Tuple2<String,Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String value) throws Exception {
                return Tuple2.of("data",1);
            }
        }).keyBy(0)
           .sum(1).print();

        env.execute();
    }
}
